package com.zzh.ideas.core.model;

import com.github.pagehelper.Page;

public class ResponseModel {

	private static final String DEFAULT_SUCCESS_MESSAGE = "操作成功";
	private static final String DEFAULT_ERROR_MESSAGE = "操作失败";
	private static final int CODE_SUCCESS = 1000;
    private static final int CODE_FAILURE_DEFAULT = 999;

	private int code = CODE_SUCCESS;
	private boolean success;
	private String message;
	private int total;// 总条数
	private int pageNum;// 当前页码
	private int pageSize;// 每页条数
	private Object data;

	private ResponseModel(int code, boolean success, String message, Object data, int total, int pageNum, int pageSize) {
	    this.code = code;
		this.success = success;
		this.message = message;
		this.data = data;
		this.total = total;
		this.pageNum = pageNum;
		this.pageSize = pageSize;
	}

    private ResponseModel(boolean success, String message, Object data, int total, int pageNum, int pageSize) {
        this(success ? CODE_SUCCESS: CODE_FAILURE_DEFAULT, success, message, data, total, pageNum, pageSize);
    }


    public ResponseModel(Page page) {
	    this(Boolean.TRUE, DEFAULT_SUCCESS_MESSAGE, page.getResult(), (int) page.getTotal(), page.getPageNum(), page.getPageSize());
    }


    public static ResponseModel success() {
		return success(DEFAULT_SUCCESS_MESSAGE, 0, 0, 0);
	}

	public static ResponseModel success(String message, int total, int pageNum, int pageSize) {
		return new ResponseModel(1000, Boolean.TRUE, message, null, total, pageNum, pageSize);
	}

	public static ResponseModel success(Object data, int total, int pageNum, int pageSize) {
		return success(null, data, total, pageNum, pageSize);
	}

	public static ResponseModel success(Object data) {
		return success(null, data, 0, 0, 0);
	}

	public static ResponseModel success(String message, Object data, int total, int pageNum, int pageSize) {
		return new ResponseModel(CODE_SUCCESS, Boolean.TRUE, message, data, total, pageNum, pageSize);
	}

	public static ResponseModel failure() {
		return failure(DEFAULT_ERROR_MESSAGE);
	}

    public static ResponseModel failure(String message) {
        return new ResponseModel(Boolean.FALSE, message, null, 0, 0, 0);
    }

	public static ResponseModel failure(int code, String message) {
		return new ResponseModel(code, Boolean.FALSE, message, null, 0, 0, 0);
	}

	public static ResponseModel failure(Object data) {
		return failure(null, data);
	}

    public static ResponseModel failure(int code, Object data) {
        return failure(code, null, data);
    }

	public static ResponseModel failure(String message, Object data) {
		return new ResponseModel(Boolean.FALSE, message, data, 0, 0, 0);
	}

    public static ResponseModel failure(int code, String message, Object data) {
        return new ResponseModel(code, Boolean.FALSE, message, data, 0, 0, 0);
    }

	public boolean isSuccess() {
		return success;
	}

	public String getMessage() {
		return message;
	}

	public Object getData() {
		return data;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Override
	public String toString() {
		return "Appr [success=" + success + ", message=" + message + ", data=" + data + ", total=" + total
				+ ", pageNum=" + pageNum + ", pageSize=" + pageSize + "]";
	}

}
